package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.UserIntegralHistoryMapper;
import com.zhongke.cotrun.model.pojo.*;
import com.zhongke.cotrun.service.IntegralOfTypeService;
import com.zhongke.cotrun.service.UserIntegralHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserIntegralHistoryServiceImpl implements UserIntegralHistoryService {
    @Autowired
    UserIntegralHistoryMapper userIntegralHistoryMapper;
    @Autowired
    IntegralOfTypeService integralOfTypeService;

    /**
     * 插入积分
     *
     * @param userIntegralHistory
     * @return
     */
    @Override
    public Boolean insertUserIntegralHistory(UserIntegralHistory userIntegralHistory) {

        return userIntegralHistoryMapper.insertUserIntegralHistory(userIntegralHistory);
    }

    /**
     * 根据用户id查看历史记录
     *
     * @param userId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectUserIntegralHistoryByUserId(Integer userId, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<UserIntegralHistoryVo> l = userIntegralHistoryMapper.selectUserIntegralHistoryByUserId(userId);

        for (UserIntegralHistoryVo uv : l) {
            resortIntegralRecordVo(uv);
        }
        PageInfo p = new PageInfo(l);
        return p;
    }

    /**
     * 根据条件查询历史记录带分页
     *
     * @param userIntegralHistory
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo seleHistoryByCndition(UserIntegralHistory userIntegralHistory, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);

        List<UserIntegralHistoryVo> l = userIntegralHistoryMapper.selectIntegralHistoryByCndition(userIntegralHistory);

        for (UserIntegralHistoryVo uv : l) {
            resortIntegralRecordVo(uv);
        }
        PageInfo p = new PageInfo(l);
        return p;
    }

    /**
     * 查询所有的积分记录
     *
     * @return
     */
    @Override
    public PageInfo selectUserIntegralHistory(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<UserIntegralHistoryVo> l = userIntegralHistoryMapper.selectAllUserIntegralHistory();
        for (UserIntegralHistoryVo uv : l) {
            resortIntegralRecordVo(uv);
        }

        PageInfo p = new PageInfo(l);

        return p;
    }

    /**
     * 查询近三个月的数据
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserIntegralHistoryVo> selctUserIntegralHistoryForThreeMounth(Integer userId) {


        return userIntegralHistoryMapper.selectThreeUserIntegralHistoryByUserId(userId);
    }

    /**
     * 查看自己的根据条件
     *
     * @param userIntegralHistory
     * @return
     */

    @Override
    public List<UserIntegralHistoryVo> selectUserIntegralHistoryOfmy(UserIntegralHistory userIntegralHistory) {
        List<UserIntegralHistoryVo> l = userIntegralHistoryMapper.selectIntegralHistoryByCndition(userIntegralHistory);
        for (UserIntegralHistoryVo uv : l) {
            resortIntegralRecordVo(uv);
        }


        return l;
    }


    /**
     * 返回规则(转换规则类型)工具封装
     */

    public UserIntegralHistoryVo resortIntegralRecordVo(UserIntegralHistoryVo uv) {

        IntegralOfType I = null;
        if (uv.getRankOneOf() != null) {
            I = integralOfTypeService.selectIntegralOfTypeByCode(uv.getRankOneOf());
        }


        if (I != null) {
            uv.setRankOneOfName(I.getOfTypeName());
        }

        if (uv.getRankTwoOf() != null) {
            I = integralOfTypeService.selectIntegralOfTypeByCode(uv.getRankTwoOf());
        }


        if (I != null) {
            uv.setRankTwoOfName(I.getOfTypeName());
        }

        return uv;
    }

    /**
     * 后台管理员查询历史记录
     *
     * @param userIntegralHistory
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo adminSelectIntegralHistory(UserIntegralHistory userIntegralHistory, Integer pageNumber, Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);
        List<UserIntegralHistoryVo> l = userIntegralHistoryMapper.adminSelectHistory(userIntegralHistory);
        if (l==null||l.isEmpty()) {
            return new PageInfo(l);
        }
        for (UserIntegralHistory uv : l) {
            UserInfo u = uv.getUserInfo();
            if ("2".equals(u.getType())) {
                u.setRealName(uv.getCompanyInfo().getCompanyName());
            }

        }

        PageInfo p = new PageInfo(l);


        return p;
    }
}
